package cn.yunhe.servlet;

import cn.yunhe.bean.Area;
import cn.yunhe.bean.Hotel;
import cn.yunhe.bean.HotelArea;
import cn.yunhe.biz.HotelService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet("/addHotelServlet")
public class addHotelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");
        List<FileItem> filelist = null ;
        try {
            filelist =  upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        String hotelName="",hotelSite="",hotelPhone="",hotelPrice="",HotelCategories="",
                hotelContext="",roomCount="",hotelarea="",hotelPic="";
        File upfile = null ;
        if(filelist !=null && filelist.size()>0){
            for (FileItem fileitem:filelist) {
                //判断是否是普通字段（true）还是文件上传字段（false）
                if(fileitem.isFormField()){
                    if("hotelName".equals(fileitem.getFieldName())){
                        hotelName = fileitem.getString("utf-8");
                    }else  if("hotelSite".equals(fileitem.getFieldName())){
                        hotelSite = fileitem.getString("utf-8");
                    }else  if("hotelPhone".equals(fileitem.getFieldName())){
                        hotelPhone = fileitem.getString("utf-8");
                    }else  if("hotelPrice".equals(fileitem.getFieldName())){
                        hotelPrice = fileitem.getString("utf-8");
                    }else  if("HotelCategories".equals(fileitem.getFieldName())){
                        HotelCategories = fileitem.getString("utf-8");
                    }else  if("hotelContext".equals(fileitem.getFieldName())){
                        hotelContext = fileitem.getString("utf-8");
                    }else  if("roomCount".equals(fileitem.getFieldName())){
                        roomCount = fileitem.getString("utf-8");
                    }else  if("hotelarea".equals(fileitem.getFieldName())){
                        hotelarea = fileitem.getString("utf-8");
                    }
                }else{
                    //获取上传文件名字
                    hotelPic =   fileitem.getName();
                    String realpath = request.getSession().getServletContext().getRealPath("/img");
                    int index = realpath.lastIndexOf("img");
                    hotelPic = realpath.substring(index)+"/"+hotelPic;

                    /*System.out.println(hotelPic+"33");
                    System.out.println("-------------"+realpath+"--------------");
                    upfile = new File(realpath,hotelPic);
                    upfile = new File("E:/ideaproject/flying_bird_travel_network/src/main/webapp/img/",hotelPic);
                    try {
                        fileitem.write(upfile);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }*/
                }
            }
        }

        Hotel hotel = new Hotel();
        hotel.setHotelName(hotelName);
        hotel.setContext(hotelContext);
        hotel.setHotelPhone(Long.parseLong(hotelPhone));
        hotel.setPic(hotelPic);
        hotel.setPrice(Integer.parseInt(hotelPrice));
        hotel.setRoomCount(Integer.parseInt(roomCount));
        hotel.setSite(hotelSite);
        hotel.setFavorite(Integer.valueOf(HotelCategories));
        HotelArea  hotelArea = new HotelArea();
        hotelArea.setHotelAreaId(Integer.parseInt(hotelarea));
        hotel.setHotelarea(hotelArea);

        HotelService hotelService = new HotelService();
        int flag =  hotelService.addhotel(hotel);
        if(flag >0 ){
            response.sendRedirect("adminPage.jsp");
        }
    }
}

